

module.exports = {
    // ctx = wx.createCanvasContext('myCanvas'),

    init(id:string, dpi:number,opt:any={w:759,h:1334}) {
        this.ctx = wx.createCanvasContext(id)
        this.canvasId = id
        this.w = opt.w
        this.h = opt.h
        // ctx = wx.createCanvasContext(id)
        this.dpi = dpi
    },
    async ctx_img(item:any) {
        const { src, x, y, w, h } = item
        let img_src = src.startsWith('http') ? await this.getImg(src) : src
        await this.ctx.drawImage(img_src, x * this.dpi, y * this.dpi, w * this.dpi, h * this.dpi)
    },

    async ctx_fill(item: any) {
        const { col, x, y, w, h } = item
        this.ctx.save()
        this.ctx.setFillStyle(col)
        this.ctx.fillRect(x * this.dpi, y * this.dpi, w * this.dpi, h * this.dpi)
        this.ctx.restore()
    },
    async ctx_text(item: any = {text:"",x:0,y:0,w:750,h:30,style: {
            size : 20,
            color : "#000",
            width : 5,
            line :'top',
            align :'left',
            rows :1
        }}) {
        const { text, x, y,w,h, style} = item
        console.log(style.color);
        this.ctx.save()

        // ctx.font = 'italic 900 20px cursive'
        this.ctx.setStrokeStyle(style.color)
        this.ctx.setFontSize(style.size * this.dpi)
        this.ctx.setLineWidth(style.width * this.dpi)
        this.ctx.setTextBaseline(style.line)
        this.ctx.setTextAlign(style.align)

        var chr = text.split("");//这个方法是将一个字符串分割成字符串数组 
        var temp = "";
        var row = [];
        
        for (var a = 0; a < chr.length; a++) {
            if (this.ctx.measureText(temp).width < w * this.dpi) {
                temp += chr[a];
            } else {
                a--; 
                //这里添加了a-- 是为了防止字符丢失，效果图中有对比
                row.push(temp);
                temp = "";
            }
        }

        row.push(temp); //如果数组长度大于2 则截取前两个
       
        console.log(style.rows);

        if (row.length > style.rows) {
            var rowCut = row.slice(0, 2);
            var rowPart = rowCut[1];
            var test = "";
            var empty = [];
            for (var a = 0; a < rowPart.length; a++) {
                if (this.ctx.measureText(test).width < w * this.dpi) {
                    test += rowPart[a];
                } else {
                    break;
                }
            }
            empty.push(test);
            var group = empty[0] + "..."//这里只显示两行，超出的用...表示
            rowCut.splice(1, 1, group);
            row = rowCut;
        }
      
        for (var b = 0; b < row.length; b++) {
            this.ctx.fillText(row[b], x * this.dpi, y * this.dpi + b * h * this.dpi, w * this.dpi)
        }


        this.ctx.restore()


        // this.ctx.fillText(text, x * this.dpi, y * this.dpi)
    },


    async getImg(src:string) {
        let img_info:any = await new Promise((res, rev) => {
            wx.getImageInfo({
                src,
                success: res,
                fail: rev
            });
        })
        return img_info.path
    },

    async begin(data = []) {

        wx.showLoading({
            title: "正在生成海报...",
            mask: true,
            success: () => {
            },
            fail: () => {},
            complete: () => {}
        });
        for (let i = 0; i < data.length; i++) {
            const element:any = data[i];
            console.log(element);
            if (element.src) {
                await this.ctx_img(element)
            } else if (element.text) {
                this.ctx_text(element)
            } else {
                this.ctx_fill(element)
            }
        }
      await  new Promise((resval, rev) => {
            this.ctx.draw(false, function () {
                resval()
            })
        })
    },
    async creat_img(opt:any) {
        let res = await new Promise((resval, rev) => {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: this.w * this.dpi,
                height: this.h * this.dpi,
                destWidth: (opt.w ? opt.w : this.w) * this.dpi,
                destHeight: (opt.h ? opt.w : this.t.w) * this.dpi,
                canvasId: this.canvasId,
                success: function (res:any) {
                    resval(res)
                    wx.showToast({
                        title: '生成成功',
                        icon: 'success',
                        image: '',
                        duration: 1500,
                        mask: false,
                        success: () => { },
                        fail: () => { },
                        complete: () => { }
                    });

                },
            })



        })
        return res.tempFilePath
    }

}



